﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace PmfWeb.Core.WebControls
{
    ///<summary>
    /// 重写表单
    ///</summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:RewriteForm runat=server></{0}:RewriteForm>")]
    public class RewriteForm : HtmlForm
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        protected override void RenderAttributes(System.Web.UI.HtmlTextWriter writer)
        {
            RewriteFormHtmlTextWriter textWriter = new RewriteFormHtmlTextWriter(writer);
            base.RenderAttributes(textWriter);
        }

        /// <summary>
        /// 
        /// </summary>
        private class RewriteFormHtmlTextWriter : HtmlTextWriter
        {
            /// <summary>
            /// 
            /// </summary>
            /// <param name="writer"></param>
            public RewriteFormHtmlTextWriter(HtmlTextWriter writer) : base(writer)
            {
                base.InnerWriter = writer.InnerWriter;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="name"></param>
            /// <param name="value"></param>
            /// <param name="fEcode"></param>
            public override void WriteAttribute(string name, string value, bool fEcode)
            {
                if(name == "action")
                {
                    HttpContext context = HttpContext.Current;
                    if(null == context.Items["ActionAlreadyWritten"])
                    {
                        value = context.Request.RawUrl;
                        context.Items["ActionAlreadyWritten"] = true;
                    }
                }
                base.WriteAttribute(name, value, fEcode);
            }
        }
    }


}